/**
 * @description 防抖函数
 * @param func 需要防抖的函数
 * @param wait 防抖时间
 * @param immediate 是否立即执行
 * @returns
 */

export function debounce(func: (...args: any[]) => any, wait: number, immediate = false) {
	let timeout: NodeJS.Timeout | null = null;
	return function (...args: any[]) {
		if (immediate) {
			func(...args);
			immediate = false;
		}
		if (timeout) {
			clearTimeout(timeout);
		}
		timeout = setTimeout(() => {
			func(...args);
		}, wait);
	};
}
